define(function(require, exports, module) {
  'use strict';

  var appRouter = require('router'),
    userProfile = require('models/user/userProfile'),
    CheckBox = require('views/widgets/checkbox');
  require('vendor/zepto.cookie');

  var LoginView = Backbone.View.extend({
    el: $('#mob-container'),
    events: {
      'click #mob-login': 'onLoginClick',
      'click #mob-regist': 'onRegistClick'
    },
    initialize: function() {
      this.render();
      this.createCheckboxs();
    },
    render: function() {
      var phoneNumber = $.fn.cookie('phoneNumber'),
        password = $.fn.cookie('password'),
        h = [];
      h.push('<div id="mob-login-page">');
      h.push('<div class="mob-header">');
      h.push('<div></div>');
      h.push('<p class="mob-header-title">集拼</p>');
      h.push('<div></div>');
      h.push('</div>');
      h.push('<div class="mob-common-page-container">');
      h.push('<div class="mob-input-box-container">');
      h.push('<i class="icons-username"></i>');
      if (_.isEmpty(phoneNumber)) {
        h.push('<input id="mob-phoneNumber" type="text" placeholder="请输入手机号码">');
      } else {
        h.push('<input id="mob-phoneNumber" type="text" placeholder="请输入手机号码" value=' + phoneNumber + '>');
      }
      h.push('</div>');
      h.push('<div class="mob-input-box-container">');
      h.push('<i class="icons-password"></i>');
      if (_.isEmpty(password)) {
        h.push('<input id="mob-password" class="mob-login-text" type="password" placeholder="请输入您的密码">');
      } else {
        h.push('<input id="mob-password" class="mob-login-text" type="password" placeholder="请输入您的密码" value=' + password + '>');
      }
      h.push('</div>');
      h.push('<div class="mob-checkboxGroups-container">');
      // h.push('<div id="mob-rememberMeCheckBox"></div>');
      h.push('<div id="mob-autoLoginCheckBox"></div>');
      h.push('</div>');
      h.push('<input id="mob-login" class="mob-button-large mob-button-green" type="button" value="登陆">');
      h.push('<input id="mob-regist" class="mob-button-large mob-button-orange" type="button" value="注册">');
      h.push('<input id="mob-forgotPassword" class="mon-button-primary" type="button" value="忘记密码？">');
      h.push('</div>');
      h.push('</div>');
      this.$el.append(h.join(''));
    },
    createCheckboxs: function() {
      new CheckBox({
        el: '#mob-autoLoginCheckBox',
        checked: $.fn.cookie('autoLogin'),
        lable: '自动登陆',
        onStateChanged: function(state) {
          $.fn.cookie('autoLogin', true, {
            expires: 14
          });
        }
      });
    },
    onLoginClick: function() {
      var self = this,
        phoneNumber = $('#mob-phoneNumber', self.el).val(),
        password = $('#mob-password', self.el).val();
      if (!_.isEmpty(phoneNumber) && !_.isEmpty(password)) {
        $.ajax({
          type: 'POST',
          url: '/shhuopu/user/app_login',
          dataType: 'json',
          data: {
            phoneNumber: phoneNumber,
            password: password
          },
          success: function(resp) {
            if (resp.isLogin) {
              if ($.fn.cookie('autoLogin')) {
                $.fn.cookie('phoneNumber', phoneNumber, {
                  expires: 14
                });
                $.fn.cookie('password', password, {
                  expires: 14
                });
              }
              userProfile.set('avatarUrl', resp.USERPIC);
              userProfile.set('username', resp.USERNAME);
              userProfile.set('userLevel', resp.USERTYPE);
              userProfile.set('vipEndTime', resp.VIPETIME);
              userProfile.set('userType', resp.USERMODEL);
              userProfile.set('realName', resp.CARDNAME);
              userProfile.set('idCardNO', resp.CARDNO);
              userProfile.set('userCity', resp.USERCITY);
              userProfile.set('companyName', resp.COMPANYNAME);
              userProfile.set('address', resp.ADDRESS);
              userProfile.set('firstTel', resp.phone1);
              userProfile.set('secondTel', resp.phone2);
            } else {

            }
          },
          error: function() {

          }
        });
      }
    },
    onRegistClick: function() {
      appRouter.navigate("setting", {
        trigger: true,
        replace: true
      });
    }

  });

  return LoginView;
});
